<?php

namespace Home\Controller;

//use Think\Controller;

class NoteController extends ExtendController {

    //
    public function index(){
        //判断是否备注
        $noteModel = D('Note');
        $noteList = $noteModel->where(array("name" => $_GET["name"]))->find();
        if(empty($noteList)){
            $this->assign('name',$_GET['name']);
            $this->display(); // 输出模板
        }else{
           $this->error('该用户已备注!');  
        }

    }


    //添加
    public function add() {
        if($_POST){
        $Note = M('Note');
        $data['u_id'] = $_SESSION['user']['user_id'];
        $data['content'] = $_POST['content'];
        $data['name'] = $_GET['name'];
        $data['date'] = date('Y-m-d H:i:s');
        $result = $Note->add($data);
        if($result){
            $this->redirect('Grade/message');
        }else{
            $this->error('非法操作!');
        }
    }else{
        $this->error('非法操作');
    }
    }
      //显示备注信息 
    public function  show_note(){
       $Note = M('Note');
       $User = M('User');
        import('ORG.Util.Page'); // 导入分页类
        $count = $Note->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();     // 分页显示输出
        $list = $Note->order('date DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($list as $key => $value) {
            $map['user_id']= $value['u_id'];
            $res = $User->where($map)->field('user_name')->find();
            $value['u_name'] = $res['user_name'];
            $list[$key] = $value;
        }
       
     $this->assign('list',$list);
      $this->display('show');
    } 



    
    //删除
    public function delete() {
        $Note = M('Note');
        $map['id'] = $_GET['id'];
        $result = $Note->where($map)->delete();
        if ($result != false) {
            $this->redirect('show_note');
        } else {
            $this->error('删除失败!');
        }
    }

}